package com.lanou.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou.entity.TbItem;
import com.lanou.service.TdItemService;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping
public class TdItemController {
    @Autowired
    public TdItemService tdItemService;


    //    @RequestMapping("/item/list")
//    @ResponseBody//分页查询所有信息(类目显示类目id)
//    public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
//        EasyUIDataGridResult result = tdItemService.getItemList(page, rows);
//        return result;
//    }
//

    //查询所有商品信息(类目显示类目名称)
    @ResponseBody
    @RequestMapping("/item/list")
    public Map<String, Object> list(Integer page, Integer rows) {
        Page<Object> startPage = PageHelper.startPage(page, rows);
        Map<String, Object> map = tdItemService.findAll();
        long total = startPage.getTotal();
        map.put("total", total);
        return map;
    }

    @RequestMapping("/item/save")
    @ResponseBody
    public Map<String, Object> addItem(TbItem tbItem,String desc,String itemParams) {

        return tdItemService.addItem(tbItem,desc,itemParams);

    }

    @ResponseBody
    @RequestMapping("/pic/upload")
    public Map<String, Object> uploadImage(@RequestParam("uploadFile") MultipartFile uploadFile, HttpServletRequest request) throws IOException {

        InputStream inputStream = uploadFile.getInputStream();

        String fileName = new Date().getTime() + ".jpg";
        Client client = Client.create();
        String path = "http://47.106.73.53/resources/image/";
        String filePath = path + fileName;
        WebResource resource = client.resource(filePath);
        resource.put(inputStream);


//        Image image = ImageIO.read(new File(filePath));
//        System.out.println("image"+image);
//        BufferedImage bi = new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB);
//        Graphics2D g2 =  bi.createGraphics();
//        g2.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null), null);
//        g2.setColor(Color.red);
//        g2.setFont(new Font("宋体",Font.BOLD,20));
//        g2.drawString("拾惠商城", 0, image.getHeight(null));
//        g2.dispose();
//        ImageIO.write(bi, "JPG", new FileOutputStream(filePath));

        Map<String, Object> map = new HashMap<>();
        map.put("url", filePath);
        map.put("error", 0);

        return map;
    }
}
